<?php


/**
 * This file contains the config class.
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @package core
 * @version 1
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * Configuration parameter management
 *
 * @package config
 * @version 1
 */
class config
{
	/**
	 * @var	array	assoc. key => value
	 */
	var $c_array = array ();
	/**
	 * @var	string	used for a module
	 */
	var $modul;
	/**
	 * @var mixed if not !== true, something is going wrong
	 */
	var $STATUS = true;
	/**
	 * @var string dir of config file
	 */
	var $path = null;
	/**
	 * Load config file into array
	 *
	 * @return     bool
	 */
	function config()
    {
		$this->path = CMSR_PATH_BASEDIR_SERVER .  WRITEABLEADMINDIR . "config/config.cfg.php";
		if (file_exists($this->path)) {
			if (!$farr = file($this->path))
				$this->STATUS = "couldn't open config file for reading";
			reset($farr);
			while (list ($k, $v) = each($farr)) {
				if (substr($v, 0, 1) != "#") {
					$v = substr($v, 0, strlen($v) - 1);
					$cfarr = split("::", $v);
					if (isset($cfarr[1]))
						$this->c_array[$cfarr[0]] = trim($cfarr[1]);
				}
			}
		} else
			return false;
		return true;
	}
	/**
	 * return the status of object
	 *
	 * @return	mixed	true or text with failure message
	 */
	function Status()
    {
		return $this->STATUS;
	}
	/**
	 * returns a config value
	 *
	 * @param      string    config item name
	 * @return     string 	 config item value
	 */
	function Get($key)
    {
		return isset ($this->c_array[strtolower($key)]) ? $this->c_array[strtolower($key)] : false;
	}
	/**
	 * save the content from array into config file and reload object
	 *
	 * @param      string    some item to skip
	 * @return     bool
	 */
	function save($skip = '')
    {
		$frontend = frontend::getInstance();
		# check if config dir exists
		if (is_object($frontend)) {
			if (!is_dir(CMSR_PATH_BASEDIR_SERVER . '/' . WRITEABLEADMINDIR . '/config'))
				if (!create_dir(CMSR_PATH_BASEDIR_SERVER . '/' . WRITEABLEADMINDIR . '/config', $frontend->getConfig()->Get('chmod_dir')))
					trigger_error('Can not create config directory', E_USER_ERROR);
		}
		@ reset($this->c_array);
		$ttl = (string) null;
		while (list ($k, $v) = @ each($this->c_array))
			if (strtolower($skip) != $k)
				$ttl .= "$k::$v\n";
		$ttl = "#<?php/*\n" . $ttl . "#*/?>";
		if (!create_file($this->path, $ttl, ($frontend->getConfig()->Get("chmod_files") ? $frontend->getConfig()->Get("chmod_files") : false)))
		{
		    $this->STATUS = "couldn't create config file: " . $this->path;
            trigger_error($this->STATUS, E_USER_ERROR);
			return false;
		}
		if ($skip) {
			unset ($this->c_array);
			return $this->config();
		}
		return true;
		$this->STATUS = "couldn't open config file for modification";
		trigger_error($this->STATUS, E_USER_ERROR);
		return false;
	}
	/**
	 * set a config item with a value
	 *
	 * @param      string    item name
	 * @param	   string    item value
	 * @return     bool
	 */
	function Set($key, $value)
    {
		$key = strtolower($key);
		$this->c_array[$key] = $value;
		return $this->save();
	}
}